package com.noble.glamour.ui.agora;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.light.UserInfoLightweight;
import com.noble.glamour.module.local.msgview.chatview.input.ChatMediaPlayer;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.util.TimeUtil;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.module.util.my.GiftHelper;
import com.noble.glamour.ui.agora.act.RtcGroupInitAct;
import com.noble.glamour.ui.agora.act.RtcInitActivity;
import com.noble.glamour.ui.agora.act.bean.RtcComment;
import com.noble.glamour.ui.agora.act.comment.RtcCommentActivity;
import com.noble.glamour.ui.agora.act.dialog.RtcFaceUnityDialog;
import com.noble.glamour.ui.agora.act.dialog.RtcVideoCommentDlg;
import com.noble.glamour.ui.agora.act.dialog.RtcVideoGiftDialog;
import com.noble.glamour.ui.agora.act.video.RtcVideoActivity;
import com.noble.glamour.ui.agora.act.voice.RtcVoiceActivity;
import com.noble.glamour.ui.agora.model.AgoraConstant;
import com.noble.glamour.ui.agora.view.RtcBottomGiftDialog;
import com.noble.library.log.PToast;

/**
 * 音视频聊天相关页面
 */
public class RtcUIShow extends UIShow{

    /**
     * 邀请，受邀，音频通信界面
     */
    public static void showRtcInitAct(Activity activity) {
        if (ChatMediaPlayer.getInstance().isPlaying()) {
            ChatMediaPlayer.getInstance().stopPlayVoice();
        }
        showActivityFromBack(RtcInitActivity.class);
    }

    /**
     * 单邀，群邀音频等待界面
     */
    public static void showRtcGroupInitAct(Activity activity) {
        if (ChatMediaPlayer.getInstance().isPlaying()) {
            ChatMediaPlayer.getInstance().stopPlayVoice();
        }
        Intent intent = new Intent(activity, RtcGroupInitAct.class);
        activity.startActivity(intent);
    }

    /**
     * 视频聊天页
     */
    public static void showRtcVideoAct(Activity activity) {
        ModuleMgr.getRtcEnginMgr().getBugSteps().add("步骤1：开始跳转视频通话页面" +
                TimeUtil.getFormatTimeChatTip(ModuleMgr.getAppMgr().getTime()));
        activity.startActivity(new Intent(activity, RtcVideoActivity.class));
    }

    public static void showRtcVideoActFinish(Activity activity) {
        showRtcVideoAct(activity);
        activity.finish();
    }

    public static void showRtcVideoAct(Activity activity, UserInfoLightweight info) {
        ModuleMgr.getRtcEnginMgr().getBugSteps().add("步骤1：开始跳转视频通话页面" +
                TimeUtil.getFormatTimeChatTip(ModuleMgr.getAppMgr().getTime()));
        Intent intent = new Intent(activity, RtcVideoActivity.class);
        intent.putExtra(AgoraConstant.USER_LIGHT_INFO_KEY, info);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * 女性任务版： 打开通话中界面
     */
    public static void showRtcVoiceAct(Activity activity) {
        activity.startActivity(new Intent(activity, RtcVoiceActivity.class));
    }

    public static void showRtcVoiceActFinish(Activity activity) {
        showRtcVoiceAct(activity);
        activity.finish();
    }

    /**
     * 视频评论页面
     */
    public static void showRtcCommentAct(final Context activity) {
        // 只有视频时弹评价结算页面
        if (ModuleMgr.getRtcEnginMgr().getEngineConfig().mChatType != AgoraConstant.RTC_CHAT_VIDEO) {
            return;
        }

        // 男性
        if (ModuleMgr.getCenterMgr().getMyInfo().isMan()) {
            showRtcCommentAct(activity, null);

//            ModuleMgr.getRtcEnginMgr().reqGetFeedbackInfo(new RequestComplete() {
//                @Override
//                public void onRequestComplete(HttpResponse response) {
//                    if (!response.isOk()) return;
//                    RtcComment comment = (RtcComment) response.getBaseData();
//                    showRtcCommentAct(activity, comment);
//                }
//            });
            return;
        }

        // 女性
        showRtcCommentAct(activity, null);
//        ModuleMgr.getRtcEnginMgr().reqGetSettlement(new RequestComplete() {
//            @Override
//            public void onRequestComplete(HttpResponse response) {
//                if (!response.isOk()) return;
//                RtcComment comment = (RtcComment) response.getBaseData();
//                showRtcCommentAct(activity, comment);
//            }
//        });
    }

    private static void showRtcCommentAct(Context activity, RtcComment comment) {
        Intent intent = new Intent(activity, RtcCommentActivity.class);
        intent.putExtra("RtcComment", comment);
        activity.startActivity(intent);
    }

    private static RtcBottomGiftDialog rtcDialog = null;

    /**
     * 送礼物底部弹框
     *
     * @param context
     * @param to_id   他人id
     */
    public static void showRtcBottomGiftDlg(final Context context, final long to_id) {
        rtcDialog = null;
        if (ModuleMgr.getCommonMgr().getGiftLists().getArrCommonGifts().size() > 0) {
            rtcDialog = new RtcBottomGiftDialog();
            rtcDialog.setToId(to_id);
            rtcDialog.setCtx(context);
            rtcDialog.showDialog((FragmentActivity) context);
            return;
        }
        ModuleMgr.getCommonMgr().requestGiftList(new GiftHelper.OnRequestGiftListCallback() {
            @Override
            public void onRequestGiftListCallback(boolean isOk) {
                if (!isOk) {
                    PToast.showShort(context.getString(R.string.net_error_retry));
                    return;
                }

                if (ModuleMgr.getCommonMgr().getGiftLists().getArrCommonGifts().size() <= 0)
                    return;
                rtcDialog = new RtcBottomGiftDialog();
                rtcDialog.setToId(to_id);
                rtcDialog.setCtx(context);
                rtcDialog.showDialog((FragmentActivity) context);
            }
        });
    }

    /**
     * 视频送礼物弹框
     */
    public static void showRtcVideoGiftDlg(final FragmentActivity context) {
        RtcVideoGiftDialog dialog = new RtcVideoGiftDialog();
        dialog.showDialog(context);
    }

    /**
     * 男用户：视频中投诉弹框
     */
    public static void showRtcVideoCommentDlg(final FragmentActivity context) {
        RtcVideoCommentDlg dialog = new RtcVideoCommentDlg();
        dialog.showDialog(context);
    }

    /**
     * FaceU道具弹框
     */
    public static void showRtcFaceUnityDialog(final FragmentActivity context) {
        RtcFaceUnityDialog dialog = new RtcFaceUnityDialog();
        dialog.showDialog(context);
    }

}
